package com.titar.robot.ui.login;


import android.app.Activity;

import com.titar.robot.AppExecutors;
import com.titar.robot.MemberInfoManager;
import com.titar.robot.db.CacheTableDao;
import com.titar.robot.db.RobotDatabase;
import com.titar.robot.entity.LoginResponse;
import com.titar.robot.entity.MemberInfo;
import com.titar.robot.http.RetrofitApi;
import com.titar.robot.http.service.CustomerService;
import com.titar.robot.repository.Resource;
import com.titar.robot.utils.SharedPreUtils;
import com.titar.robot.utils.ToastUtils;

import androidx.lifecycle.Observer;

public class LoginPresenter implements LoginContact.Presenter {

    private Activity activity;
    private LoginContact.View v;
    private LoginRepository loginRepository;

    public LoginPresenter() {
        this.activity = activity;
        AppExecutors appExecutors = AppExecutors.getInstence();
        CacheTableDao cacheTableDao = RobotDatabase.getDatabase().cacheTableDao();
        CustomerService customerService = RetrofitApi.getRequestInterface();
        loginRepository = new LoginRepository(appExecutors, cacheTableDao, customerService);
    }

    @Override
    public void login(String phone, String password) {
        loginRepository.login(phone, password)
                .observeForever(new Observer<Resource<LoginResponse>>() {
                    @Override
                    public void onChanged(Resource<LoginResponse> stringResource) {
                        switch (stringResource.getStatus()) {
                            case ERROR:
                                if (v != null) {
                                    v.dismissLoadingDialog();
                                    v.showErrorDialog(stringResource.getMessage(), false);
                                }
                                break;
                            case RESULT_ERROR:
                                if (v != null) {
                                    v.dismissLoadingDialog();
                                    ToastUtils.showToast(stringResource.getMessage());
                                }
                                break;
                            case LOADING:
                                if (v != null) {
                                    v.showLoadingDialog();
                                }
                                break;
                            case SUCCESS:
                                if (v != null) {
                                    SharedPreUtils.getInstence(activity).setToken(stringResource.getData().getToken());
                                    //保存登录成功token
                                    v.dismissLoadingDialog();
                                    v.onLoginSucess(stringResource.getData());
                                }
                                break;
                        }
                    }
                });
    }

    @Override
    public void getMemberInfo(String token) {
        loginRepository.getMemberInfo(token)
                .observeForever(new Observer<Resource<MemberInfo>>() {
                    @Override
                    public void onChanged(Resource<MemberInfo> memberInfoResource) {
                        switch (memberInfoResource.getStatus()) {
                            case ERROR:
                                if (v != null) {
                                    v.dismissLoadingDialog();
                                    v.showErrorDialog(memberInfoResource.getMessage(), false);
                                }
                                break;
                            case RESULT_ERROR:
                                if (v != null) {
                                    v.dismissLoadingDialog();
                                    ToastUtils.showToast(memberInfoResource.getMessage());
                                }
                                break;
                            case LOADING:
                                if (v != null) {
                                    v.showLoadingDialog();
                                }
                                break;
                            case SUCCESS:
                                if (v != null) {
                                    //保存登录成功token
                                    v.dismissLoadingDialog();
                                    MemberInfoManager.setLoginMemberInfo(memberInfoResource.getData());
                                    v.onGetMemberInfoSucess(memberInfoResource.getData());
                                }
                                break;
                        }
                    }
                });
    }

    @Override
    public LoginPresenter attachView(LoginContact.View view) {
        this.v = view;
        return this;
    }

    @Override
    public void detachView() {
        this.v = null;
    }
}
